package cn.neusoft.luofa603.yuejian_29.activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.List;

import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.adapter.ClubSearchAdapter;
import cn.neusoft.luofa603.yuejian_29.adapter.TrainSearchAdapter;
import cn.neusoft.luofa603.yuejian_29.bean.Club;
import cn.neusoft.luofa603.yuejian_29.bean.Training;
import cn.neusoft.luofa603.yuejian_29.model.ClubModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ClubSearchActivity extends AppCompatActivity {
    private ImageView back,club_search;
    private EditText search_editText;
    private RecyclerView recyclerView;
    private String search_key;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clubsearch);
        initViews();
        initEvent();
        initData();
    }
    public void initViews(){
        back=(ImageView)findViewById(R.id.back);
        club_search=(ImageView)findViewById(R.id.club_search);
        search_editText=(EditText)findViewById(R.id.search_editText);
        recyclerView = (RecyclerView) findViewById(R.id.rv_club_search);
        recyclerView.setLayoutManager(new LinearLayoutManager(ClubSearchActivity.this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }
    public void  initEvent(){
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        club_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                club_search();
            }
        });

    }
    public void initData(){
        search_key=search_editText.getText().toString();
    }
    public void club_search(){
        ClubModel clubModel = new ClubModel();
        Call<List<Club>> call = clubModel.clubsearch("club",search_key);
        call.enqueue(new Callback<List<Club>>() {
            @Override
            public void onResponse(Call<List<Club>> call, Response<List<Club>> response) {
                final List<Club> mDataList = response.body();
                ClubSearchAdapter clubSearchAdapter=new ClubSearchAdapter(ClubSearchActivity.this,mDataList);
                recyclerView.setAdapter(clubSearchAdapter);
                clubSearchAdapter.setItemClickListener(new ClubSearchAdapter.MyItemClickListener(){
                    public void onItemClick(View view, int position) {
                        Intent intent = new Intent(ClubSearchActivity.this,ClubInfoActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("club",mDataList.get(position));
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });

            }

            @Override
            public void onFailure(Call<List<Club>> call, Throwable t) {

            }
        });
    }
}
